package com.sinoglobal.dumping.adapter;

import java.text.SimpleDateFormat;
import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sinoglobal.dumping.R;
import com.sinoglobal.dumping.base.Dumpling_DateUtils;
import com.sinoglobal.dumping.bean.myDumpling.Dumpling_CashDumplingDetailBean.Dumpling_CashDumplingDetail;

public class Dumpling_CashDumplingDetailAdapter extends BaseAdapter {

	private List<Dumpling_CashDumplingDetail> datas;
	private Context context;

	public Dumpling_CashDumplingDetailAdapter(Context context) {
		this.context = context;
	}

	@Override
	public int getCount() {

		return datas == null ? 0 : datas.size();
	}

	@Override
	public Object getItem(int position) {

		if (datas != null) {
			return datas.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {

		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;

		if (convertView == null || convertView instanceof RelativeLayout) {
			viewHolder = new ViewHolder();
			convertView = View.inflate(context,
					R.layout.dumpling_item_cash_dumpling_detail, null);
			viewHolder.tv_who_get_dumpling = (TextView) convertView
					.findViewById(R.id.tv_who_get_dumpling);
			viewHolder.tv_get_dumpling_date = (TextView) convertView
					.findViewById(R.id.tv_get_dumpling_date);
			viewHolder.v_bottom_line = (TextView) convertView.findViewById(R.id.v_bottom_line);
			viewHolder.iv_ok= (ImageView) convertView.findViewById(R.id.iv_ok);
			viewHolder.item_root_view = (LinearLayout) convertView.findViewById(R.id.item_root_view);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		Dumpling_CashDumplingDetail dumpling_CashDumplingDetail= datas.get(position);
//		viewHolder.v_bottom_line.setVisibility(position == getCount() - 1 ? View.VISIBLE : View.GONE);
		viewHolder.tv_who_get_dumpling.setText(changePhoneStyle(dumpling_CashDumplingDetail.phone) + "捞到了, 已领取" + dumpling_CashDumplingDetail.prizeAmount + "元!");
		viewHolder.tv_get_dumpling_date.setText(Dumpling_DateUtils.format24Time(dumpling_CashDumplingDetail.laoquDate));
		viewHolder.iv_ok.setVisibility("最佳手气".equals(dumpling_CashDumplingDetail.ok)?View.VISIBLE:View.INVISIBLE);
		
//		if (position == 0) {
//			viewHolder.item_root_view.setBackgroundColor(context.getResources().getColor(R.color.my_cash_dumpling_detail_good_bg));
//			viewHolder.iv_ok.setVisibility(View.VISIBLE);
//		}
		viewHolder.item_root_view.setBackgroundColor("最佳手气".equals(dumpling_CashDumplingDetail.ok) ? context.getResources().getColor(R.color.dumpling_my_cash_dumpling_detail_good_bg):context.getResources().getColor(R.color.dumpling_my_cash_dumpling_detail_bg));
		return convertView;
	}

	public static String actionTimeOfDay(String time) {
		// if (Dumpling_TextUtil.stringIsNull(time)) {
		// return "";
		// }
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		long t = Long.valueOf(time);
		String data = sdf.format(t*1000);
		return data;
	}

	public void setData(List<Dumpling_CashDumplingDetail> datas) {
		this.datas = datas;
		notifyDataSetChanged();
	}

	public static class ViewHolder {
		private TextView tv_who_get_dumpling;
		private TextView tv_get_dumpling_date;
		private TextView v_bottom_line ;
		private ImageView iv_ok ;
		private LinearLayout item_root_view;
	}
	
	private String changePhoneStyle(String phone){
		String result = "" ;
		if (!TextUtils.isEmpty(phone)) {
			result = phone.substring(0, 3) + "****" + phone.substring(7, phone.length());
		}
		return result ;
	}
}
